package class

import (
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"project/pkg/mildware"
	"project/pkg/models"
	"project/pkg/validate"
	"strconv"
	"strings"
)

func (h CHandler) SearchClassInfoByTeacherID(c *gin.Context) {
	var classes []*models.ClassInfo
	resp := new(models.RespData)
	req := new(models.SearchClassInfoByTeacherIDReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Student:SearchClassInfoByTeacherID with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Student:SearchClassInfoByTeacherID with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	//查找教师
	exist, info, err := h.M.TeacherCheckExistByID(strconv.Itoa(req.TeacherId))
	if err != nil {
		zap.L().Error("Class:SearchClassInfoByTeacherID h.M.TeacherCheckExistByID err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	if exist {
		//查找班级
		classList := strings.Split(info.ClassList, "|")
		for _, v := range classList {
			if v == "" {
				continue
			}
			//查找班级
			cexist, classInfo, err := h.M.ClassCheckExistByID(v)
			if err != nil {
				zap.L().Error("Class:SearchClassInfoByTeacherID h.M.ClassCheckExistByID err", zap.Error(err))
				resp.SendServerBusy(c)
				return
			}
			if cexist {
				classes = append(classes, classInfo)
			}
		}
	}
	resp.Data = classes
	resp.SendSuccess(c)
}
func (h CHandler) SearchClassInfoByTeacherIDToGet(c *gin.Context) {
	var classes []*models.ClassInfo
	resp := new(models.RespData)
	page, size, err := models.GetPageInfo(c)
	if err != nil {
		zap.L().Error("Class:SearchClassInfoByTeacherID models.GetPageInfo err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	//token获得id
	id, _, _, err := mildware.GetCyrrentUserID(c)
	if err != nil {
		zap.L().Error("Class:SearchClassInfoByTeacherID mildware.GetCyrrentUserID err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	//查找教师
	exist, info, err := h.M.TeacherCheckExistByID(id)
	if err != nil {
		zap.L().Error("Class:SearchClassInfoByTeacherID h.M.TeacherCheckExistByID err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	if exist {
		//查找班级
		classList := strings.Split(info.ClassList, "|")
		for _, v := range classList {
			if v == "" {
				continue
			}
			//查找班级
			cexist, classInfo, err := h.M.ClassCheckExistByID(v)
			if err != nil {
				zap.L().Error("Class:SearchClassInfoByTeacherID h.M.ClassCheckExistByID err", zap.Error(err))
				resp.SendServerBusy(c)
				return
			}
			if cexist {
				classes = append(classes, classInfo)
			}
		}
	}
	//实现分页
	nowIndex := (page - 1) * size // 3 -- 0 1 2 : 3 4 5
	count := len(classes)
	resp.Count = int64(count)
	if nowIndex+size >= count {
		resp.Data = classes[nowIndex:count]
	} else if nowIndex > count {
		resp.SendSuccessToLayUi(c)
		return
	} else {
		resp.Data = classes[nowIndex : nowIndex+size]
	}
	resp.SendSuccessToLayUi(c)
}
